﻿using NCP.Recruiting.ApplicationServices.Interfaces;
using NCP.Recruiting.ApplicationServices.Messaging.ShortMessages;
using NCP.Recruiting.ApplicationServices.ViewModels;
using NCP.Recruiting.Domain.Aggregates.ShortMessages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NCP.Recruiting.ApplicationServices.Implementations
{
    public class ShortMessageAppService : IShortMessageAppService
    {
        private readonly IShortMessageRepository _shortMessageRepository;

        public ShortMessageAppService(IShortMessageRepository shortMessageRepository)
        {
            this._shortMessageRepository = shortMessageRepository;
        }
        public CreateShortMessageResponse CreateShortMessage(CreateShortMessageRequest request)
        {
            CreateShortMessageResponse response = new CreateShortMessageResponse();

            ShortMessage shortMessage = new ShortMessage
            {
                CompanyId = request.ShortMessageProperties.CandidateId,
                LastUpdated = DateTime.Now,
                Deleted = false,
                CandidateId = request.ShortMessageProperties.CandidateId,
                Content = request.ShortMessageProperties.Content,
                DateCreated = DateTime.Now,
                DateSent = DateTime.Now,
                ResumeId = request.ShortMessageProperties.ResumeId,
                Subject = request.ShortMessageProperties.Subject
            };
            shortMessage.GenerateNewIdentity();

            try
            {
                ThrowExceptionIfShortMessageIsInvalid(shortMessage);

                _shortMessageRepository.Add(shortMessage);
                _shortMessageRepository.UnitOfWork.Commit();
            }
            catch (Exception ex)
            {
                response.AddError(ex.Message);
            }

            return response;
        }

        private void ThrowExceptionIfShortMessageIsInvalid(ShortMessage shortMessage)
        {
            // 业务检查/验证
        }


        public DeleteShortMessageResponse DeleteShortMessage(DeleteShortMessageRequest request)
        {
            DeleteShortMessageResponse response = new DeleteShortMessageResponse();

            try
            {
                ShortMessage shortMessage = _shortMessageRepository.Get(request.Id);

                if (shortMessage != null)
                {
                    shortMessage.Deleted = true;
                    shortMessage.LastUpdated = DateTime.Now;

                    _shortMessageRepository.Modify(shortMessage);
                    _shortMessageRepository.UnitOfWork.Commit();
                }
                else
                {
                    response.AddError("请求的ShortMessage不存在");
                }
            }
            catch (Exception ex)
            {
                response.AddError(ex.Message);
            }

            return response;
        }

        public GetShortMessagesByCompanyResponse GetShortMessagesByCompany(GetShortMessagesByCompanyRequest request)
        {
            var response = new GetShortMessagesByCompanyResponse();

            try
            {
                var specifications = ShortMessageSpecifications.GetShortMessagesByCompany(request.Id);
                var list = _shortMessageRepository.GetPagedList(request.PageIndex, request.PageSize, specifications, x => x.DateSent, false);

                var result = list.Select(x => new ShortMessageListItemModel()
                 {
                     Id = x.Id,
                     Applicant = "abc",
                     AppliedJob = "IT软件工程师",
                     Subject = "邀请面试",
                     Content = x.Content,
                     DateSent = x.DateSent
                 });
                
            }
            catch (Exception ex)
            {
                response.AddError(ex.Message);
            }

           
            return response;
        }
    }
}
